package com.zxy.Answer.scoring;

import com.zxy.Answer.common.ErrorCode;
import com.zxy.Answer.exception.BusinessException;
import com.zxy.Answer.model.entity.App;
import com.zxy.Answer.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评分策略执行器
 * 声明式注解，启动配置类
 *  对比if..else../switch方式
 *
 * @author zxy
 * @since 2024/6/4
 **/
@Service
public class ScoringStrategyExecutor {
    // 评分策略列表
    @Resource
    private List<ScoringStrategy> strategyList;

    public UserAnswer doScore(List<String> choiceList, App app) {
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        if (appType == null || scoringStrategy == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
        }
        for (ScoringStrategy strategy : strategyList) {
            if (strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)) {
                ScoringStrategyConfig strategyConfig = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if (strategyConfig.appType() == appType && strategyConfig.scoringStrategy() == scoringStrategy)
                    return strategy.doScore(choiceList, app);
            }
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，未找到匹配的策略");
    }
}
